package com.soulkun.smarthome.logic

import com.soulkun.smarthome.logic.convert.SoulkunGsonConverterFactory
import com.soulkun.smarthome.logic.interceptor.AntiReplayInterceptor
import com.soulkun.smarthome.logic.interceptor.SignatureInterceptor
import com.soulkun.smarthome.logic.interceptor.TokenInterceptor
import okhttp3.OkHttpClient
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object ServiceCreator {

    fun <T> apiServiceCreate(baseUrl: String, serviceClass: Class<T>): T =
        Retrofit.Builder()
            .baseUrl(baseUrl)
            .client(
                OkHttpClient.Builder()
                    .addInterceptor(TokenInterceptor())
                    .addInterceptor(AntiReplayInterceptor())
                    .connectTimeout(10, TimeUnit.SECONDS)
                    .build()
            )
            .addConverterFactory(SoulkunGsonConverterFactory.create())
            .build()
            .create(serviceClass)

    fun <T> commonServiceCreate(baseUrl: String, serviceClass: Class<T>): T =
        Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(serviceClass)

    inline fun <reified T> apiRetrofitCreator(baseUrl: String): T = apiServiceCreate(baseUrl, T::class.java)
    inline fun <reified T> commonRetrofitCreator(baseUrl: String): T = commonServiceCreate(baseUrl, T::class.java)

}